﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Calendar;
using Quartz.Impl.Matchers;

namespace QuartzDemo
{
    public class SayHelloJob : IJob
    {
        public string UserName { get; set; }

        public Task Execute(IJobExecutionContext context)
        {
            return Task.Factory.StartNew(() =>
            {
                Console.WriteLine($"Hello {UserName}!");
            });
        }
    }

    public class MyJobListener : IJobListener
    {
        public string Name { get; } = nameof(MyJobListener);

        public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            //Job即将执行
            return Task.Factory.StartNew(() =>
            {
                Console.WriteLine($"Job: {context.JobDetail.Key} 即将执行");
            });
        }

        public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            return Task.Factory.StartNew(() => {
                Console.WriteLine($"Job: {context.JobDetail.Key} 被否决执行");
            });
        }

        public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default)
        {
            //Job执行完成
            return Task.Factory.StartNew(() =>
            {
                Console.WriteLine($"Job: {context.JobDetail.Key} 执行完成");
            });
        }
    }

    class Program
    {
        static async Task Main()
        {
            var schedulerFactory = new StdSchedulerFactory();
            var scheduler = await schedulerFactory.GetScheduler();
            await scheduler.Start();
            scheduler.ListenerManager.AddJobListener(new MyJobListener(), GroupMatcher<JobKey>.AnyGroup());
            Console.WriteLine($"任务调度器已启动");

            //创建作业和触发器
            var jobDetail = JobBuilder.Create<SayHelloJob>()
                                        .SetJobData(new JobDataMap() {
                                new KeyValuePair<string, object>("UserName", "Tom")
                                        })
                                        .Build();

            //var trigger = TriggerBuilder.Create()
            //                            .WithSimpleSchedule(m => {
            //                                m.WithRepeatCount(3).WithIntervalInSeconds(1);
            //                            })
            //                            .Build();

            //var trigger = TriggerBuilder.Create()
            //                            .WithCronSchedule("*/1 * * * * ?")
            //                            .Build();

            var calandar = new HolidayCalendar();
            calandar.AddExcludedDate(DateTime.Today);

            await scheduler.AddCalendar("holidayCalendar", calandar, false, false);

            var trigger = TriggerBuilder.Create()
                                    .WithCronSchedule("*/1 * * * * ?")
                                    //.ModifiedByCalendar("holidayCalendar")
                                    .Build();

            //添加调度
            await scheduler.ScheduleJob(jobDetail, trigger);
            Console.ReadLine();
        }
    }
}
